package com.dnjn.happiness.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.domain.Advice;
import com.dnjn.happiness.biz.domain.AdviceImage;
import com.dnjn.happiness.biz.domain.AdviceThumbsUp;
import com.dnjn.happiness.biz.domain.AdviceVo;
import com.dnjn.happiness.biz.domain.dto.StatisticalAnalysisDTO;
import com.dnjn.happiness.biz.domain.vo.StatisticalAnalysisAppriseVO;
import com.dnjn.happiness.biz.domain.vo.StatisticalAnalysisVO;
import com.dnjn.happiness.biz.mapper.AdviceImageMapper;
import com.dnjn.happiness.biz.mapper.AdviceMapper;
import com.dnjn.happiness.biz.mapper.AdviceThumbsUpMapper;
import com.dnjn.happiness.biz.service.IAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 建议Service业务层处理
 *
 * @author happiness
 * @date 2023-10-14
 */
@Service
public class AdviceServiceImpl extends ServiceImpl<AdviceMapper, Advice> implements IAdviceService {
    /**
     * 建议状态
     */
    public static final String ADVICESTATUSNO = "0";
    public static final String ADVICESTATUSYES = "1";
    @Autowired
    AdviceMapper adviceMapper;
    @Autowired
    AdviceImageMapper adviceImageMapper;
    @Autowired
    AdviceThumbsUpMapper adviceThumbsUpMapper;

    @Override
    public Page<AdviceVo> selectPageX(Advice domain, Page page) {
        return baseMapper.selectPageX(domain, page);
    }

    @Override
    public void getAdviceImageAndThumps(List<AdviceVo> adviceList) {
        if (adviceList.size() > 0) {
            adviceList.stream().forEach(advice -> {
                Long adviceId = advice.getAdviceId();
                QueryWrapper<AdviceImage> wrapper = new QueryWrapper<>();
                wrapper.eq("advice_id", adviceId);
                List<AdviceImage> adviceImages = adviceImageMapper.selectList(wrapper);
                advice.setImages(adviceImages);
//                QueryWrapper<AdviceThumbsUp> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("advice_id",adviceId);
//                List<AdviceThumbsUp> adviceThumbsUps =adviceThumbsUpMapper.selectList(queryWrapper);
                List<AdviceThumbsUp> adviceThumbsUps = adviceThumbsUpMapper.queryLeftByUser(adviceId);

                //组装
                advice.setAdviceThumbsUps(adviceThumbsUps);
            });
        }
    }

    @Override
    public int UpdateOper(Advice domain) {
        int count = adviceMapper.UpdateOper(domain.getOperUserId(), domain.getAdviceId());
        return count;
    }

    @Override
    public int updateAdviceById(Long adviceId, Long thumsCount) {
        return adviceMapper.upDateAdviceStatusById(adviceId, thumsCount);
    }

    @Override
    public List<Advice> queryAssign(Long adviceId) {
        List<Advice> advice = adviceMapper.selectByAdviceId(adviceId);
        return advice;
    }

    @Override
    public Integer getAdviceThumpsById(Long userId) {

        return adviceMapper.getAdviceThumpsById(userId);
    }

    @Override
    public Advice selectById(Long adviceId) {

        return adviceMapper.selectAdviceById(adviceId);
    }

    @Override
    public List<StatisticalAnalysisVO> selectStatisticalAnalysisAdviceList(StatisticalAnalysisAppriseVO vo) {
        return baseMapper.selectStatisticalAnalysisAdviceList(vo);
    }

    @Override
    public Double modelUp(Long userId, String time) {
        return baseMapper.modelUp(userId, time);
    }

    @Override
    public Double modelOper(Long userId, String time) {
        return baseMapper.modelOper(userId, time);
    }

    @Override
    public List<StatisticalAnalysisVO> selectStatistical(StatisticalAnalysisDTO dto, Long deptId) {
        return baseMapper.selectStatistical(dto, deptId);
    }



    @Override
    public Page<StatisticalAnalysisVO> selectStatisticalAnalysisAdvice(Long adviceTagsId, Integer count, String symbol, Page page) {
        return baseMapper.selectStatisticalAnalysisAdvice(adviceTagsId, count, symbol, page);
    }

    @Override
    @Transactional
    public int saveAdvice(AdviceVo advice) {
        advice.setAdviceStatus(ADVICESTATUSNO);
        int insert = adviceMapper.insert(advice);
        Long adviceId = advice.getAdviceId();
//        List<AdviceImage> images = advice.getImages();
        List<String> images = advice.getUrl();
        if (images != null && !images.isEmpty()) {
            images.stream().forEach(image -> {

                AdviceImage adviceImage = new AdviceImage();

                adviceImage.setAdviceId(adviceId);
//               adviceImage.setImageUrl(image.getImageUrl());
                adviceImage.setImageUrl(image);
                adviceImageMapper.insert(adviceImage);

            });
        }
        return insert;
    }

    @Override
    @Transactional
    public List<AdviceVo> queryByUserId(Advice domain) {

        ArrayList<AdviceVo> list = new ArrayList<>();
        QueryWrapper<Advice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", domain.getUserId())
                .eq(domain.getAdviceTagsId() != null, "advice_tags_id", domain.getAdviceTagsId())
                .eq(domain.getOperUserId() != null, "oper_user_id", domain.getOperUserId());
        List<Advice> adviceList = adviceMapper.selectList(queryWrapper);


        if (adviceList.size() > 0) {
            adviceList.stream().forEach(advice -> {
                AdviceVo vo = new AdviceVo();
                Long adviceId = advice.getAdviceId();
                QueryWrapper<AdviceImage> wrapper = new QueryWrapper<>();
                wrapper.eq("advice_id", adviceId);
                List<AdviceImage> adviceImages = adviceImageMapper.selectList(wrapper);

                vo.setImages(adviceImages);

                QueryWrapper<AdviceThumbsUp> objectQueryWrapper = new QueryWrapper<>();
                QueryWrapper<AdviceThumbsUp> id = objectQueryWrapper.eq("advice_id", adviceId);
                List<AdviceThumbsUp> adviceThumbsUps = adviceThumbsUpMapper.selectList(id);
                //组装
                vo.setAdviceThumbsUps(adviceThumbsUps);
                vo.setAdviceId(adviceId);
                vo.setAdviceStatus(advice.getAdviceStatus());
                vo.setAdviceText(advice.getAdviceText());
                vo.setIsAnonymous(advice.getIsAnonymous());
                vo.setUserId(advice.getUserId());
                vo.setUsername(advice.getUsername());
                vo.setOperUserId(advice.getOperUserId());
                vo.setOperUsername(advice.getOperUsername());
                list.add(vo);


            });
        }
        return list;
    }

    @Override
    @Transactional
    public List<AdviceVo> listQueryUserTag(Advice domain) {

        //所有的模块
        ArrayList<AdviceVo> list = new ArrayList<>();
        List<Long> tagIds = adviceMapper.selectTagIdFromUserTag(domain.getUserId());
        if (tagIds.size() > 0) {
            List<Advice> adviceList = adviceMapper.selectAdviceInTagIds(tagIds);
            adviceList.stream().forEach(advice -> {

                AdviceVo vo = new AdviceVo();
                Long adviceId = advice.getAdviceId();
                QueryWrapper<AdviceImage> wrapper = new QueryWrapper<>();
                wrapper.eq("advice_id", adviceId);
                List<AdviceImage> adviceImages = adviceImageMapper.selectList(wrapper);

                vo.setImages(adviceImages);

                QueryWrapper<AdviceThumbsUp> objectQueryWrapper = new QueryWrapper<>();
                QueryWrapper<AdviceThumbsUp> id = objectQueryWrapper.eq("advice_id", adviceId);
                List<AdviceThumbsUp> adviceThumbsUps = adviceThumbsUpMapper.selectList(id);
                //组装
                vo.setAdviceThumbsUps(adviceThumbsUps);
                vo.setAdviceId(adviceId);
                vo.setAdviceStatus(advice.getAdviceStatus());
                vo.setAdviceText(advice.getAdviceText());
                vo.setIsAnonymous(advice.getIsAnonymous());
                vo.setUserId(advice.getUserId());
                vo.setUsername(advice.getUsername());
                vo.setOperUserId(advice.getOperUserId());
                vo.setOperUsername(advice.getOperUsername());
                list.add(vo);

            });
        }
        return list;
    }


    /**
     * 添加图片和建议
     *
     * @param advice
     * @return
     */
//    @Override
//    @Transactional
//    public int saveAdvice(Advice advice) {
//        int i = baseMapper.insert(advice);
//        Long adviceId = advice.getAdviceId();
////        List<Object> list = new ArrayList<>();
//        List<String> urlList = advice.getUrl();
//        if(urlList!=null && !urlList.isEmpty()){//有图片
//            //非空 =>遍历->save
//            urlList.stream().forEach(url->{
//                AdviceImage adviceImage = new AdviceImage();
//                adviceImage.setAdviceId(adviceId);
//                adviceImage.setImageUrl(url);
//                //保存 关联关系  这个是一个一个添加  没有批量添加  批量添加创建一个list saveBatch
//                 adviceImageMapper.insert(adviceImage);
//            });
//        }
//
//        return i;
//
//    }
    @Override
    public Page<AdviceVo> getPage(Advice domain, Page page) {
        return baseMapper.getPage(domain, page);
    }
}
